package auth

import (
	"admin/dao/auth"
	"admin/dto"
	"admin/modules/auth/logic"
	"admin/utils"
	"admin/utils/response"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"strconv"
)

type RolesController struct {
	roleLogic *logic.RolesLogic
}

func NewRolesController(db *gorm.DB) *RolesController {
	roleMenuRepo := auth.NewRoleMenuRepo(db)
	rolePermissionRepo := auth.NewRolePermissionRepo(db)
	roleRepo := auth.NewRoleRepo(db)
	permissionRepo := auth.NewPermissionRepo(db)
	menuRepo := auth.NewMenuRepo(db)
	return &RolesController{
		roleLogic: logic.NewRolesLogic(roleRepo, roleMenuRepo, rolePermissionRepo, permissionRepo, menuRepo),
	}
}

// List 获取角色列表
func (ro *RolesController) List(c *gin.Context) {
	// 获取权限ID
	req, _ := utils.BindJson[dto.RoleListDto](c)
	list, err := ro.roleLogic.List(c, req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "获取角色列表失败")
		return
	}
	response.OK(c, gin.H{"list": list}, "success", "成功")
}

// Create 创建角色
func (ro *RolesController) Create(c *gin.Context) {
	req, _ := utils.BindJson[dto.RoleDto](c)
	if err := ro.roleLogic.Create(c, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "创建角色失败")
		return
	}
	response.OK(c, gin.H{}, "success", "创建角色成功")
}

// Update 更新角色
func (ro *RolesController) Update(c *gin.Context) {
	id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.RoleDto](c)
	if err := ro.roleLogic.Update(c, id, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "更新角色失败")
		return
	}
	response.OK(c, gin.H{}, "success", "更新角色成功")
}

// delete 删除角色
func (ro *RolesController) Delete(c *gin.Context) {
	id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	if err := ro.roleLogic.Delete(c, id); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "删除角色失败")
		return
	}
	response.OK(c, gin.H{}, "success", "删除角色成功")
}

// GetPermissionsByRoleId 通过角色 id 获取权限
func (ro *RolesController) GetPermissionsByRoleId(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	permissions, err := ro.roleLogic.GetPermissionsByRoleId(c, roleId)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "获取权限失败")
		return
	}
	response.OK(c, gin.H{"list": permissions}, "success", "成功")
}

// AddPermissionToRole 给角色添加权限
func (ro *RolesController) AddPermissionToRole(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.AssignPermissionToRoleDto](c)
	if err := ro.roleLogic.AddPermissionToRole(c, roleId, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "添加权限失败")
		return
	}
	response.OK(c, gin.H{}, "success", "添加权限成功")
}

// RemovePermissionFromRole 删除角色的某个权限
func (ro *RolesController) RemovePermissionFromRole(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.AssignPermissionToRoleDto](c)
	if err := ro.roleLogic.RemovePermissionFromRole(c, roleId, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "删除权限失败")
		return
	}
}

// GetMenusByRoleId	通过角色 id 获取菜单
func (ro *RolesController) GetMenusByRoleId(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	menus, err := ro.roleLogic.GetMenusByRole(roleId)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "获取菜单失败")
		return
	}
	response.OK(c, gin.H{"list": menus}, "success", "成功")
}

// AddMenuToRole 给角色添加权限
func (ro *RolesController) AddMenuToRole(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.AssignMenuToRoleDto](c)
	if err := ro.roleLogic.AddMenusToRole(c, roleId, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "添加权限失败")
		return
	}
	response.OK(c, gin.H{}, "success", "添加权限成功")
}

// RemoveMenuFromRole 删除角色的某个权限
func (ro *RolesController) RemoveMenuFromRole(c *gin.Context) {
	roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.AssignMenuToRoleDto](c)
	if err := ro.roleLogic.RemoveMenusFromRole(c, roleId, req); err != nil {
		response.Error(c, http.StatusInternalServerError, 1001, err.Error(), "删除权限失败")
		return
	}
}
